package com.jh.base;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.StringUtils;
import com.jh.base.utils.FastClickUtil;

/**
 * @author Mr.JH
 * @class name：BaseActivity1
 * 视代码为粪土 为了赚钱不惜沾满一手粪土
 * @date 2022/4/7
 * @Description: 
 */
public
abstract class BaseActivity1 extends BaseActivity implements View.OnClickListener {
    private ViewGroup mToolbar;
    private ViewGroup ll_toolbar_center;
    protected TextView tv_title;
    private TextView mTvBack;
    protected TextView mTvGoto;

    @Override
    protected View installToolBarCenter() {
        View toolContent = installCustomToolContent();
        if (toolContent == null) {
            mToolbar = (ViewGroup) View.inflate(this, R.layout.activity_base1, null);
        } else {
            mToolbar = (ViewGroup) toolContent;
        }
        ll_toolbar_center = mToolbar.findViewById(R.id.ll_toolbar_center);
        View view = installToolBarCenter2();
        if (view == null) {
            tv_title = mToolbar.findViewById(R.id.tv_title);
            if (tv_title != null) {
                tv_title.setText(StringUtils.isEmpty(setToolbarTitle()) ? "" : setToolbarTitle());
                if (setToolbarTitleDrawableId() != null) {
                    Drawable nav_up = getResources().getDrawable(setToolbarTitleDrawableId());
                    nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                    tv_title.setCompoundDrawables(null, null, nav_up, null);
                    tv_title.setCompoundDrawablePadding(10);
                } else {
                    tv_title.setCompoundDrawables(null, null, null, null);
                }
            }
        } else {
            ll_toolbar_center.removeAllViews();
            ll_toolbar_center.addView(view);
        }

        mTvBack = mToolbar.findViewById(R.id.tv_back_base1);
        mTvBack.setOnClickListener(this);

        mTvGoto = mToolbar.findViewById(R.id.tv_goto_base1);
        mTvGoto.setOnClickListener(this);
        mTvGoto.setText("");
        return mToolbar;
    }

    protected View installToolBarCenter2() {
        return null;
    }

    protected View installCustomToolContent() {
        return null;
    }

    protected abstract String setToolbarTitle();

    protected Integer setToolbarTitleDrawableId() {
        return null;
    }

    /**
     * @param right_id  标题资源
     * @param isVisible 是否显示
     * @Author :Mr.JH
     * @Date :2022/4/7 14:44
     * @Description: 标题点击
     */
    public void setTitleGoto(Integer right_id, Boolean isVisible) {
        if (isVisible) {
            tv_title.setOnClickListener(this);
        }
        if (right_id != null) {
            Drawable nav_up = getResources().getDrawable(right_id);
            nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
            tv_title.setCompoundDrawables(null, null, nav_up, null);
        } else {
            tv_title.setCompoundDrawables(null, null, null, null);
        }
    }

    /**
     * @param text      提示文字
     * @param right_id  图片资源
     * @param isVisible 是否显示
     * @Author :Mr.JH
     * @Date :2022/4/7 14:41
     * @Description: 给toolbar添加进入图标
     */
    public void setToolbarGoto(String text, Integer right_id, Boolean isVisible) {
        mTvGoto.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        mTvGoto.setText(StringUtils.isEmpty(text) ? "" : text);
        if (right_id != null) {
            Drawable nav_up = getResources().getDrawable(right_id);
            int width = (int) getResources().getDimension(R.dimen.db_20);
            nav_up.setBounds(0, 0, width, width);
            mTvGoto.setCompoundDrawables(null, null, nav_up, null);
        } else {
            mTvGoto.setCompoundDrawables(null, null, null, null);
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getVisibility() == View.VISIBLE) {
            if (FastClickUtil.isFastClick()) {
                //不能连续点击
                return;
            }
            int id = view.getId();
            if (id == R.id.tv_back_base1) {
                //返回键
                back();
            } else if (id == R.id.tv_title) {
                //标题点击
                gotoTitleNext();
            } else if (id == R.id.tv_goto_base1) {
                //复杂顶部导航栏跳转，比如搜索
                gotoNest();
            }
        }
    }

    /**
     * @Author :Mr.JH
     * @Date :2022/4/7 14:49
     * @Description: 返回
     */
    protected void back() {
        KeyboardUtils.hideSoftInput(BaseActivity1.this);
        finish();
    }

    /**
     * @Author :Mr.JH
     * @Date :2022/4/7 14:48
     * @Description: 更多功能点击
     */
    protected void gotoNest() {
    }

    /**
     * @Author :Mr.JH
     * @Date :2022/4/7 14:48
     * @Description: 标题点击
     */
    protected void gotoTitleNext() {
    }
}
